#include "stdafx.h"
#include "Resource.h"

Resource* Resource::_instance = 0;

Resource* Resource::Instance()
{
	if (_instance == 0)
	{
		_instance = new Resource();
	}
	return _instance;
}

Resource::Resource()
{
	al_add_new_bitmap_flag(ALLEGRO_MAG_LINEAR);
	readBmpInformation();
}

void Resource::readBmpInformation()
{
	string tempStr;
	infile.open("gameinformation/bmpInformation.txt");
	if (!infile)
	{
		//error
		exit(1);
	}
	while (getline(infile, tempStr))
	{
		int x, y, w, h, num;
		string a, temp, name;
		stringstream ss(tempStr);
		ss >> a;
		while (ss >> temp >> name >> x >> y >> w >> h >> num)
		{
			if (!al_create_child_bitmap(temp, name, x, y, w, h, num))
			{
				cout << "can't create the bitmap";
			}			
		}
		ss.clear();
	}
	infile.close();
}

ALLEGRO_BITMAP* Resource::getBitmap(string str, int i)
{
	if (str == "Logo")
	{
		return _arrLogo[i];
	}
	if (str == "Icon")
	{
		return _arrIcon[i];
	}
	return NULL;
}

bool Resource::al_create_child_bitmap(string str, string name, int x,
									  int y, int w, int h, int num)
{
	if (str == "Logo")
	{
		ALLEGRO_BITMAP* bitmap = al_load_bitmap(name.c_str());
		if (!bitmap)
		{
			cout << "error1";
		}

		_allLogo.push_back(bitmap);
		for (int i = 0; i < num; i++)
		{
			_arrLogo.push_back(al_create_sub_bitmap(bitmap, x, y, w, h));
			x += w;
		}
		return true;
	}else 
	if (str == "Icon")
	{
		ALLEGRO_BITMAP* bitmap = al_load_bitmap(name.c_str());
		if (!bitmap)
		{
			//error
		}
		_allIcon.push_back(bitmap);
		for (int i = 0; i < num; i++)
		{
			_arrIcon.push_back(al_create_sub_bitmap(bitmap, x, y, w, h));
			x += w;
		}
		return true;
	}else 
	if (str == "player")
	{
		ALLEGRO_BITMAP* bitmap = al_load_bitmap(name.c_str());
		if (!bitmap)
		{
			//error
		}
		_allPlayer.push_back(bitmap);
		for (int i = 0; i < num; i++)
		{
			_arrPlayer.push_back(al_create_sub_bitmap(bitmap, x, y, w, h));
			x += w;
		}
		return true;
	}else 
	if (str == "enemy")
	{
		ALLEGRO_BITMAP* bitmap = al_load_bitmap(name.c_str());
		if (!bitmap)
		{
			//error
		}
		_allEmeny.push_back(bitmap);
		for (int i = 0; i < num; i++)
		{
			_arrEmeny.push_back(al_create_sub_bitmap(bitmap, x, y, w, h));
			x += w;
		}
		return true;
	}else 
	if (str == "bullet")
	{
		ALLEGRO_BITMAP* bitmap = al_load_bitmap(name.c_str());
		if (!bitmap)
		{
			cout << "can't open the bullet picture" << endl;
		}
		_allBullet.push_back(bitmap);
		for (int i = 0; i < num; i++)
		{
			_arrBullet.push_back(al_create_sub_bitmap(bitmap, x, y, w, h));
			x += w;
		}
		return true;
	}else 
	if (str == "burst")
	{
		ALLEGRO_BITMAP* bitmap = al_load_bitmap(name.c_str());
		if (!bitmap)
		{
			cout << "cam't open the bullet picture" << endl;
		}
		_allBurst.push_back(bitmap);
		for (int i = 0; i < num; i++)
		{
			_arrBurst.push_back(al_create_sub_bitmap(bitmap, x, y ,w, h));
			x += w;
		}
		return true;
	}else 
	if (str == "item")
	{
		ALLEGRO_BITMAP* bitmap = al_load_bitmap(name.c_str());
		if (!bitmap)
		{
			cout << "can't load the item bitmap" << endl;
		}
		_allItem.push_back(bitmap);
		for (int i = 0; i < num; i++)
		{
			_arrItem.push_back(al_create_sub_bitmap(bitmap, x, y ,w, h));
			x += w;
		}
		return true;
	}else if (str == "backGround")
	{
		ALLEGRO_BITMAP* bitmap = al_load_bitmap(name.c_str());
		if (!bitmap)
		{
			cout << "can't load the item bitmap" << endl;
		}
		_backGround.push_back(bitmap);
		return true;
	}else if (str == "blend")
	{
		ALLEGRO_BITMAP* bitmap = al_load_bitmap(name.c_str());
		if (!bitmap)
		{
			cout << "can't load the blend bitmap" << endl;
		}
		_blend.push_back(bitmap);
		return true;
	}else if (str == "praticle")
	{
		ALLEGRO_BITMAP* bitmap = al_load_bitmap(name.c_str());
		if (!bitmap)
		{
			cout << "can't load the praticle" << endl;
		}
		_praticle.push_back(bitmap);
		return true;
	}
	return false;
}

Resource::~Resource()
{
	_bmpInformation.clear();
	deleteBitmap(_arrLogo);
	deleteBitmap(_arrIcon);
	deleteBitmap(_arrBullet);
	deleteBitmap(_allIcon);
	deleteBitmap(_allLogo);
	deleteBitmap(_allBullet);
	_instance = 0;
}

void Resource::deleteBitmap(vector<ALLEGRO_BITMAP*>& v)
{
	for (vector<ALLEGRO_BITMAP*>::iterator itor = v.begin();
		itor != v.end(); itor++)
	{
		al_destroy_bitmap(*itor);
	}
	v.clear();
}